package com.spider.common.task.controller;

import com.alibaba.fastjson.JSONObject;
import com.spider.common.bean.Category;
import com.spider.common.bean.ganji.GjHousePhoneVo;
import com.spider.common.task.service.AgentTask;
import com.spider.common.task.service.GanjiGrabTaskService;
import com.spider.common.utils.ExportTextUtil;
import com.spider.common.utils.JedisUtils;
import com.spider.common.utils.R;
import com.spider.common.utils.StringUtils;
import com.spider.common.web.service.CategoryService;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author marver
 * @Description
 * @date 2018/12/17 12:29
 */
@Controller
public class GradTaskController {
    @Autowired
    private GanjiGrabTaskService ganjiGrabTaskService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private AgentTask agentTask;

    /**
     * 采集分类
     * @param type
     */
    @RequestMapping("/grabTask")
    public void grabTask(String url, Integer type){
        if(type==1){
            ganjiGrabTaskService.getHouse(url==null?"https://sz.58.com":url, type);
        }else if(type==2){
            ganjiGrabTaskService.getHouse(url==null?"http://sz.ganji.com":url,type);
        }
    }

    @RequestMapping("/")
    public String index(){
        //agentTask.getProxyTask();
        return "index";
    }

    /**
     * 采集电话
     * @param categoryId
     * @param page
     */
    @RequestMapping("/grabPhone")
    @ResponseBody
    public R grabPhone(Long categoryId, Integer page){
        Category category = categoryService.queryCategoryById(categoryId);

        String pageUrl = "";
        if(category.getType()==1){
            if(page!=1){
                if(pageUrl.contains("?")){
                    pageUrl = category.getUrl().replace("?","pn"+page+"/?");
                }else{
                    pageUrl = category.getUrl()+"pn"+page;
                }
            }else{
                pageUrl = category.getUrl();
            }
        }else if(category.getType()==2){
            if(page!=1){
                pageUrl = category.getUrl()+"/pn"+page;
            }else{
                pageUrl = category.getUrl();
            }
        }
        List<String> list = ganjiGrabTaskService.redisHouseDetail(pageUrl);
        if(CollectionUtils.isEmpty(list)){
            return R.error("该分类采集失败，请更换分类进行采集");
        }
        //List<String> list = ganjiGrabTaskService.getHouseDetail(pageUrl);
//        List<GjHousePhoneVo> phoneVos = ganjiGrabTaskService.queryPhoneInfo(list);
        Set<GjHousePhoneVo> phoneVos = ganjiGrabTaskService.queryPhoneInfos(list);
        getJedis(phoneVos);
        return R.ok("采集完成").put("phoneVos",phoneVos);
    }


    /**
     * 采集电话导出
     * @param response
     * @param
     */
    @RequestMapping("/exportTxt")
    public void grabPhone(HttpServletResponse response,String nowDate){
        if(StringUtils.isBlank(nowDate)){
            nowDate = DateUtils.formatDate(new Date());
        }
        String content = JedisUtils.get(nowDate+":phone");
        if(StringUtils.isBlank(content)){
            content = "暂无数据！";
        }else {
            List<GjHousePhoneVo> phoneVos = JSONObject.parseArray(content, GjHousePhoneVo.class);
            content = "";
            for (int i = 0; i < phoneVos.size(); i++) {
                content += phoneVos.get(i).getPhone()+"\r";
            }
        }
        ExportTextUtil.writeToTxt(response,content,"采集的电话号码");
    }

    /**
     * redis数据存取
     * @param phoneVos
     */
    public void getJedis(Set<GjHousePhoneVo> phoneVos){
        String nowDate = DateUtils.formatDate(new Date());
        String content = JedisUtils.get(nowDate+":phone");
        if(StringUtils.isBlank(content)){
            JedisUtils.set(nowDate+":phone",JSONObject.toJSON(phoneVos).toString(),0);
        }else {
            List<GjHousePhoneVo> phoneVoList = JSONObject.parseArray(content, GjHousePhoneVo.class);
            phoneVos.addAll(phoneVoList);
            JedisUtils.set(nowDate+":phone",JSONObject.toJSON(phoneVos).toString(),0);
        }
    }

    /**
     * 清空分类采集的redis数据
     * @param
     * @param
     */
    @RequestMapping("/delRedisCategoryUrl")
    @ResponseBody
    public R delRedisCategoryUrl(){
        String nowDate = DateUtils.formatDate(new Date());
        String unVisitedUrl = nowDate+":houseDetails:unVisitedUrl";
        JedisUtils.del(unVisitedUrl);
        return R.ok();
    }
}
